package com.sanjiahantang.ythshopping.adapter.home;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.ythshopping.R;
import com.sanjiahantang.ythshopping.activity.GoodsListActivity;
import com.sanjiahantang.ythshopping.activity.me.WebActivity;
import com.sanjiahantang.ythshopping.config.ConfigValue;
import com.sanjiahantang.ythshopping.model.BrandBeanModel;
import com.sanjiahantang.ythshopping.model.Brands;
import com.sanjiahantang.ythshopping.presenter.BrandBeanPresenter;
import com.sanjiahantang.ythshopping.views.BrandBeanView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/6/8.
 */

public class BrandHolder extends MyBaseViewHolder {
    private Context context;

    private TextView tv_more;
    private ImageView iv_big;
    private MyGridView gridView;

    private List<Brands> datas;

    private List<Brands> partDatas = new ArrayList<>();

    private BrandBeanPresenter brandBeanPresenter;

    public BrandHolder(View itemView, final Context context, final List<Brands> datas) {
        super(itemView);
        this.context = context;
        this.datas = datas;
        tv_more = (TextView) itemView.findViewById(R.id.tv_more);
        iv_big = (ImageView) itemView.findViewById(R.id.iv_big);
        gridView = (MyGridView) itemView.findViewById(R.id.home_brand_grid);
        tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, GoodsListActivity.class);
                intent.putExtra("type", ConfigValue.TYPE_BRANDS);
                context.startActivity(intent);
            }
        });
        iv_big.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, WebActivity.class);
                intent.putExtra("url", datas.get(6).getAd_link());
                context.startActivity(intent);
            }
        });
    }

    @Override
    void bindData() {
        if (datas != null) {
            //加载第一张图片
            Picasso.with(context).load(ConfigValue.IMG_IP + datas.get(6).getImage()).into(iv_big);
            List<Brands> brand = datas.subList(0, 6);

            gridView.setAdapter(new GvBrandAdapter(context, brand));

        } else {
            brandBeanPresenter = new BrandBeanPresenter(new Brand());
            brandBeanPresenter.loadBrandData(context);
        }

    }

    class Brand implements BrandBeanView {
        @Override
        public void brandBeanData(BrandBeanModel brandBeanModel) {

//            datas = brandBeanModel.getDatas();

            if (datas != null) {
                //加载第一张图片
                Picasso.with(context).load(datas.get(0).getBrand_logo()).into(iv_big);

//                partDatas = datas.subList(1, 7);
                gridView.setAdapter(new GvBrandAdapter(context, partDatas));
            }
        }
    }
}
